package cn.edu.scau.cmi.liangzaoqing.util;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import cn.edu.scau.cmi.liangzaoqing.database.model.Project;

public class XmlToBeanUtil {
    
    /**
     * xml文件配置转换为对象
     * @param xmlPath  xml文件路径
     * @param load    java对象.Class
     * @return    java对象
     * @throws JAXBException    
     * @throws IOException
     */
    public static Object xmlToBean(String xmlPath,Class<?> load) throws JAXBException, IOException{
        JAXBContext context = JAXBContext.newInstance(load);  
        Unmarshaller unmarshaller = context.createUnmarshaller(); 
        Object object = unmarshaller.unmarshal(new File(xmlPath));
        return object;
    }
    
    public static Project getProject(String physicalPKXmlFilePath) throws JAXBException, IOException{
        Project project = new Project();
        if(physicalPKXmlFilePath!=null && !physicalPKXmlFilePath.trim().isEmpty()) {
        	Object object = XmlToBeanUtil.xmlToBean(physicalPKXmlFilePath,Project.class);
            project = (Project)object;
        }else {
        	project=null;
        }
        return project;
    }
}
//package cn.edu.scau.cmi.liangzaoqing.util;
//
//import java.io.File;
//import java.io.IOException;
//import java.util.List;
//
//import javax.xml.bind.JAXBContext;
//import javax.xml.bind.JAXBException;
//import javax.xml.bind.Unmarshaller;
//
//import cn.edu.scau.cmi.liangzaoqing.database.model.Project;
//
//public class XmlToBeanUtil {
//    
//    /**
//     * xml文件配置转换为对象
//     * @param xmlPath  xml文件路径
//     * @param load    java对象.Class
//     * @return    java对象
//     * @throws JAXBException    
//     * @throws IOException
//     */
//    public static Object xmlToBean(String xmlPath,Class<?> load) throws JAXBException, IOException{
//        JAXBContext context = JAXBContext.newInstance(load);  
//        Unmarshaller unmarshaller = context.createUnmarshaller(); 
//        Object object = unmarshaller.unmarshal(new File(xmlPath));
//        return object;
//    }
//    
//    public static void main(String[] args) throws IOException, JAXBException {
//        String xmlPath =  "ema.xml";
//        Object object = XmlToBeanUtil.xmlToBean(xmlPath,Project.class);
//        Project project = (Project)object;
//        System.out.println(project.toString());
//        System.out.println(project.getModel().toString());
//        System.out.println(project.getModel().getDatabase().getTables().toString());
//    }
//}